package com.nervenets.web.controller.handler.user;

import com.nervenets.utils.StringUtils;
import com.nervenets.web.controller.handler.Handler;
import com.nervenets.web.controller.handler.common.PagingParams;
import com.nervenets.web.enumeration.MovieReleaseStatus;
import com.nervenets.web.enumeration.MovieStatus;
import com.nervenets.web.enumeration.MovieType;
import com.nervenets.web.enumeration.NewsType;
import com.nervenets.web.hibernate.domain.film.Movie;
import com.nervenets.web.hibernate.domain.news.News;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class HomeHandler extends Handler<PagingParams> {
    @Override
    public String handle(PagingParams params, HttpServletRequest request) throws Exception {
        List<NewsModel> newsModels = new ArrayList<>();
        List<NoticeModel> noticeModels = new ArrayList<>();
        if (0 == params.getStart()) {
            String hql = "from News where type = ? and hot is true order by id desc ";
            List<Object> objects = new ArrayList<>();
            objects.add(NewsType.normal);
            List<News> hotNews = userDao.getAllObjectsByHql(hql, objects, 0, 5);
            for (News hotNew : hotNews) {
                if (!StringUtils.isEmpty(hotNew.getInfo().getBigCoverImage())) {
                    newsModels.add(new NewsModel(hotNew));
                }
            }

            hql = "from News where type = ? order by id desc ";
            objects = new ArrayList<>();
            objects.add(NewsType.notice);
            List<News> noticeNews = userDao.getAllObjectsByHql(hql, objects, 0, 5);
            for (News noticeNew : noticeNews) {
                noticeModels.add(new NoticeModel(noticeNew));
            }
        }

        String hql = "from Movie where disable = 0 order by queue desc ";
        List<Object> objects = new ArrayList<>();
        List<Movie> movies = userDao.getAllObjectsByHql(hql, objects, params.getStart(), params.getLimit());
        List<MovieModel> movieModels = new ArrayList<>();
        for (Movie movie : movies) {
            movieModels.add(new MovieModel(movie, globalService.getMovieBuyMoney(movie.getId())));
        }

        return successMessage(movieModels
                , new KeyValue("news", newsModels)
                , new KeyValue("notices", noticeModels)
        );
    }

    @Override
    public String result() throws Exception {
        return successMessage("影片列表", new KeyValue("news", "热点资讯列表"), new KeyValue("notices", "系统公告列表"));
    }

    class NewsModel {
        private long newsId;
        private String title;
        private String bigCoverImage;

        public NewsModel(News news) {
            this.newsId = news.getId();
            this.title = news.getInfo().getTitle();
            this.bigCoverImage = news.getInfo().getBigCoverImage();
        }
    }

    class NoticeModel {
        private long newsId;
        private String title;

        public NoticeModel(News news) {
            this.newsId = news.getId();
            this.title = news.getInfo().getTitle();
        }
    }

    class MovieModel {
        private long movieId;
        private String title;
        private String subtitle;
        private String coverImage;
        private long totalMoney;
        private long saleMoney;
        private long priceOfUnit;
        private int userLimit;
        private MovieType movieType;
        private MovieStatus movieStatus;
        private MovieReleaseStatus releaseStatus;
        private boolean valid;

        public MovieModel(Movie movie, long saleMoney) {
            this.movieId = movie.getId();
            this.title = movie.getInfo().getTitle();
            this.subtitle = movie.getInfo().getSubtitle();
            this.coverImage = movie.getInfo().getCoverImage();
            this.totalMoney = movie.getTotalOfMoney();
            this.saleMoney = saleMoney;
            this.priceOfUnit = movie.getInfo().getPriceOfUnit();
            this.userLimit = movie.getInfo().getUserLimit();
            this.movieType = movie.getType();
            this.movieStatus = movie.getStatus();
            this.releaseStatus = movie.getReleaseStatus();
            this.valid = movie.getInfo().isValid();
        }
    }
}
